home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3 / CHAPTE13 / CRTDIBSN.C < prev    next >
C/C++ Source or Header  |  1996-01-31  |  7KB  |  215 lines

  1.  
  2. #include <windows.h>  
  3. #include "CrtDIBSn.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  22.  
  23. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  24.                       LPTSTR lpCmdLine, int nCmdShow)
  25. {
  26.    MSG      msg;
  27.    HWND     hWnd; 
  28.    WNDCLASS wc;
  29.  
  30.    // Register the main application window class.
  31.    //............................................
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    // Create the main application window.
  54.    //....................................
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  108. {
  109. static HBITMAP hBitmap = NULL;
  110. static LPTSTR  lpBits = NULL;
  111.  
  112.    switch( uMsg )
  113.    {
  114.       case WM_CREATE :
  115.               {
  116.                   BITMAPINFOHEADER  bi;
  117.                   BITMAPINFOHEADER* lpbi;
  118.                   RGBQUAD*          lpQuad;
  119.                   HBITMAP           hBitmap;
  120.                   HDC               hDC;
  121.                   HANDLE            hDIB;
  122.                   int               i;
  123.  
  124.                   // Initialize the BITMAPINFOHEADER structure.
  125.                   //...........................................
  126.                   bi.biSize     = sizeof( BITMAPINFOHEADER );
  127.                   bi.biWidth    = 50;
  128.                   bi.biHeight   = 50;
  129.                   bi.biPlanes   = 1;
  130.                   bi.biBitCount = 4;
  131.                   bi.biCompression   = BI_RGB;
  132.                   bi.biSizeImage     = 25 * 50; //(width / pixels per byte) 
  133.                                                 // * height
  134.                   bi.biXPelsPerMeter = 0;
  135.                   bi.biYPelsPerMeter = 0;
  136.                   bi.biClrUsed       = 0;
  137.                   bi.biClrImportant  = 0;
  138.  
  139.                   hDIB = GlobalAlloc( GHND, sizeof( BITMAPINFOHEADER )+
  140.                                                     16 * sizeof( RGBQUAD ) );
  141.                   lpbi = (BITMAPINFOHEADER*)GlobalLock( hDIB );
  142.  
  143.                   *lpbi = bi;
  144.                   lpQuad = &((LPBITMAPINFO)lpbi)->bmiColors[0];
  145.  
  146.                   for ( i = 0; i < 16; i++ )
  147.                   {
  148.                      lpQuad->rgbBlue  = i*16; 
  149.                      lpQuad->rgbRed   = 0;
  150.                      lpQuad->rgbGreen = 0;
  151.  
  152.                      lpQuad++;
  153.                   }
  154.  
  155.                   hDC = GetDC( hWnd );
  156.  
  157.                   // Create DIB.
  158.                   //............
  159.                   hBitmap = CreateDIBSection( hDC, (LPBITMAPINFO)lpbi,
  160.                                          DIB_RGB_COLORS, &lpBits, NULL, 0 );
  161.  
  162.                   ReleaseDC( hWnd, hDC );
  163.                   GlobalUnlock( hDIB );
  164.                   GlobalFree( hDIB );
  165.               }
  166.               break;
  167.  
  168.       case WM_COMMAND :
  169.               switch( LOWORD( wParam ) )
  170.               {
  171.                  case IDM_ABOUT :
  172.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  173.                         break;
  174.  
  175.                  case IDM_EXIT :
  176.                         DestroyWindow( hWnd );
  177.                         break;
  178.               }
  179.               break;
  180.       
  181.       case WM_DESTROY :
  182.               PostQuitMessage(0);
  183.               break;
  184.  
  185.       default :
  186.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  187.    }
  188.  
  189.    return( 0L );
  190. }
  191.  
  192.  
  193. LRESULT CALLBACK About( HWND hDlg,           
  194.                         UINT message,        
  195.                         WPARAM wParam,       
  196.                         LPARAM lParam)
  197. {
  198.    switch (message) 
  199.    {
  200.        case WM_INITDIALOG: 
  201.                return (TRUE);
  202.  
  203.        case WM_COMMAND:                              
  204.                if (   LOWORD(wParam) == IDOK         
  205.                    || LOWORD(wParam) == IDCANCEL)    
  206.                {
  207.                        EndDialog(hDlg, TRUE);        
  208.                        return (TRUE);
  209.                }
  210.                break;
  211.    }
  212.  
  213.    return (FALSE); 
  214. }
  215.